﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#include "stdafx.h"
#include "resource.h"
#include "ServerLoader.h"
#include "AuthorDBDlg.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// CAuthorDBDlg
XBEGIN_DLG_ID_WND_EXCHANGE( CAuthorDBDlg )
XDLG_ID_WND( IDC_CONNECT_CENTER,  m_ConnectCenter )
XDLG_ID_WND( IDC_LISTEN_INTERNAL, m_ListenInternal )
XDLG_ID_WND( IDC_AUTHORDB_INFO,   m_ServerInfo )
XDLG_ID_WND( IDD_AUTHOR,          m_AuthorSvr )
XEND_DLG_ID_WND_EXCHANGE( CXDlg )

CAuthorDBDlg::CAuthorDBDlg(void)
: m_uAuthorDBServer(0)
, m_uAuthorDBQuery(0)
, m_uAuthorDBLoad(0)
, m_uAuthorOnline(0)
, m_uAuthorTotal(0)
{
}

CAuthorDBDlg::~CAuthorDBDlg(void)
{
}

LRESULT CAuthorDBDlg::OnInitDialog(void)
{
    RECT rc;
    GetClientRect(&rc);
    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);
    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);

    m_AuthorSvr.MoveWindow(0, rc.top, rc.right, (rc.bottom - rc.top));
    //
    CStringFix strTemp;
    strTemp.Load(IDC_CONNECT_CENTER);
    m_ConnectCenter.SetWindowText(*strTemp);

    strTemp.Load(IDC_LISTEN_INTERNAL);
    m_ListenInternal.SetWindowText(*strTemp);
    //
    InitServerData(PAKT_AUTHOR);
    return TRUE;
}

LRESULT CAuthorDBDlg::OnSize(WPARAM, LPARAM lParam, BOOL&)
{
    RECT rc   = { 0 };
    rc.right  = (Long)LOWORD(lParam);
    rc.bottom = (Long)HIWORD(lParam);

    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);
    m_ServerInfo.MoveWindow(0, rc.top, rc.right, DLG_ITEM_HEIGHT);

    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);
    m_AuthorSvr.MoveWindow(0, rc.top, rc.right, (rc.bottom - rc.top));

    rc.right /=2;
    if (rc.right > DLG_ITEM_WIDTH)
    {
        m_ConnectCenter.MoveWindow(0, 0, (rc.right - DLG_ITEM_GRAP), (DLG_ITEM_GRAP + DLG_ITEM_HEIGHT));
        m_ListenInternal.MoveWindow((rc.right + DLG_ITEM_GRAP), 0, (rc.right - DLG_ITEM_GRAP), (DLG_ITEM_GRAP + DLG_ITEM_HEIGHT));
    }
    return 0;
}

void CAuthorDBDlg::OnLive(bool bStart)
{
    CStringFix strTemp;
    strTemp.Load(IDC_LISTEN_INTERNAL);
    
    if (bStart)
    {
        CStrAddr strAddr;
        GServerLoaderInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_AUTHORDB, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);
        m_ListenInternal.SetWindowText(*strTemp);
    }
    else
    {
        m_ListenInternal.SetWindowText(*strTemp);

        strTemp.Load(IDC_CONNECT_CENTER);
        m_ConnectCenter.SetWindowText(*strTemp);

        m_uAuthorDBServer = 0;
        m_uAuthorDBQuery  = 0;
        m_uAuthorDBLoad   = 0;
        m_uAuthorOnline   = 0;
        m_uAuthorTotal    = 0;
    }
    m_ServerInfo.SetWindowText(TF(""));
}

void CAuthorDBDlg::OnLink(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_CENTER)
    {
        CStringFix strTemp;
        strTemp.Load(IDC_CONNECT_CENTER);

        CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

        CStrAddr   strAddr;
        GServerLoaderInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_CENTER, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);

        GServerLoaderInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
        strTemp.AppendFormat(TF("([%s]:%d)"), *strAddr.strIp, strAddr.usPort);

        m_ConnectCenter.SetWindowText(*strTemp);
    }
    else
    {
        AddServerData(nServerIndex, utData);
    }
}

void CAuthorDBDlg::OnUpdate(Int nServerIndex, uintptr_t utData)
{
    UpdateServerData(nServerIndex, utData);
}

void CAuthorDBDlg::OnSync(Int nServerIndex, uintptr_t utData)
{
    PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
    if (nServerIndex == PAKT_AUTHOR)
    {
        m_uAuthorTotal    = pStatus->uTotal;
        m_uAuthorOnline   = pStatus->uOnline;
    }
    else
    {
        m_uAuthorDBServer = pStatus->uTotal;
        m_uAuthorDBQuery  = pStatus->uOnline;
        m_uAuthorDBLoad   = pStatus->usLoad / DATAD_TOPERCENT;
    }
}

void CAuthorDBDlg::OnSync(Int, CStream&)
{
}

void CAuthorDBDlg::OnUnlink(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_CENTER)
    {
        CStringFix strTemp;
        strTemp.Load(IDC_CONNECT_CENTER);
        m_ConnectCenter.SetWindowText(*strTemp);
    }
    else
    {
        RemoveServerData(nServerIndex, utData);
    }
}

void CAuthorDBDlg::UpdateServerInfo(void)
{
    CStringFix strInfo;
    strInfo.Load(IDC_AUTHORDB_INFO);

    CStringFix strTemp;
    strTemp.Format(*strInfo, m_uAuthorDBServer, m_uAuthorDBQuery, m_uAuthorDBLoad,
                   m_uAuthorOnline, m_uAuthorTotal);
    m_ServerInfo.SetWindowText(*strTemp);
}

CXListView* CAuthorDBDlg::GetListView(Int nServerIndex)
{
    UNREFERENCED_PARAMETER(nServerIndex);
    assert(nServerIndex == PAKT_AUTHOR);
    return &m_AuthorSvr;
}

